package erwiki.admin.internal;

import org.osgi.framework.BundleContext;
//import org.elwiki.admin.app.internal.bundle.AdminAppUiActivator;
//import org.elwiki.api.IApplicationSession;
//import org.elwiki.api.IElWikiSession;
//import org.elwiki.api.IUserManager;
//import org.elwiki.api.authorization.IAuthorizer;
//import org.elwiki.api.authorization.IGroupWiki;
//import org.elwiki.api.authorization.ISessionMonitor;
//import org.elwiki.api.authorization.user.IUserDatabase;
//import org.elwiki.api.authorization.user.UserProfile;
//import org.elwiki.api.exceptions.NoSuchPrincipalException;
//import org.elwiki.api.exceptions.WikiSecurityException;
//import org.elwiki.data.authorize.WikiPrincipal;
//import org.elwiki.utils.TextUtil;
//import org.osgi.service.useradmin.Group;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;

import erwiki.api.auth.AccountManager;
import erwiki.api.auth.AccountRegistry;
import erwiki.api.core.Engine;
import erwiki.api.exceptions.NoSuchPrincipalException;
import erwiki.api.exceptions.WikiSecurityException;

public class InitializeLoginDb {

	public static final String ADMIN_ID = "admin";
	public static final String ADMIN_NAME = "Administrator";
	public static final String ADMIN_GROUP = "Admin";

	// == CODE ================================================================

	/**
	 * See if the admin user exists already.</br>
	 * Returns <code>true</code> if the administrative user had been created previously.
	 * 
	 * @return the result
	 */
	public static boolean isAdminExists() {
		// workaround - получить сервис Engine.
		BundleContext bundleContext = FrameworkUtil.getBundle(InitializeLoginDb.class).getBundleContext();
	    ServiceReference<Engine> serviceReference = bundleContext.getServiceReference(Engine.class);
	    Engine engine = (Engine) bundleContext.getService(serviceReference);

	    //:FVK: старое - новое:
	    //IUserManager userMgr = engine.getManager(AccountManager.class);
	    AccountManager userMgr = engine.getManager(AccountManager.class);
	    //IUserDatabase userDb = engine.getManager(UserDatabase.class);
		AccountRegistry userDb = engine.getManager(AccountRegistry.class);

		try {
			userDb.findByLoginName(ADMIN_ID);
			return true;
		} catch (NoSuchPrincipalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * Creates an administrative user and Admin group. Returns the new password of administrator.</br>
	 * If the admin user exists, the password will be <code>null</code>.
	 * 
	 * @return the password
	 * @throws WikiSecurityException
	 */
	public static String createAdministrator() throws WikiSecurityException {
		// workaround - получить сервис Engine.
		BundleContext bundleContext = FrameworkUtil.getBundle(InitializeLoginDb.class).getBundleContext();
	    ServiceReference<Engine> serviceReference = bundleContext.getServiceReference(Engine.class);
	    Engine engine = (Engine) bundleContext.getService(serviceReference);

	    //:FVK: старое - новое:
	    //IUserManager userMgr = engine.getManager(AccountManager.class);
	    AccountManager userMgr = engine.getManager(AccountManager.class);
	    //IUserDatabase userDb = engine.getManager(UserDatabase.class);
		AccountRegistry userDb = engine.getManager(AccountRegistry.class);
	    
		String password = null;
/*:FVK: TODO: E4 */		
//		IApplicationSession appSession = AdminAppUiActivator.getDefault().getAppSession();
//		IUserManager userMgr = appSession.getUserManager();
//		IUserDatabase userDb = userMgr.getUserDatabase();

		// See if the admin user exists already?
		try {
			userDb.findByLoginName(ADMIN_ID);
		} catch (NoSuchPrincipalException e) {
			// Create a random 12-character password
			/*TODO: сделать для новой E4.
			password = TextUtil.generateRandomPassword();
			UserProfile profile = userDb.newProfile();
			profile.setLoginName(ADMIN_ID);
			profile.setFullname(ADMIN_NAME);
			profile.setPassword(password);
			userDb.saveProfile(profile);
			*/
		}

		/*TODO: сделать для новой E4.
		
		// Create a new admin group
		IAuthorizer groupMgr = appSession.getGroupManager();
		IGroupWiki group = null;
//		try {
//			group = groupMgr.getGroup(ADMIN_GROUP);
//			group.add(new WikiPrincipal(ADMIN_NAME));
//		} catch (NoSuchPrincipalException e) {
//:FVK:		group = groupMgr.parseGroup(null, ADMIN_GROUP, ADMIN_NAME, true);
//		}

		UISession uiSession = RWT.getUISession();
		IElWikiSession session = appSession.getElWikiSession(uiSession);
//:FVK:		groupMgr.setGroup(session, group);

*/
		return password;
	}
}
